package com.xms.schema.api.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xms.core.model.PagedList;
import com.xms.schema.entity.OptionSet;
import com.xms.schema.service.optionset.IOptionSetDetailFinderService;
import com.xms.schema.service.optionset.IOptionSetFinderService;
import com.xms.utils.StringUtil;
import com.xms.web.framework.controller.ApiBaseController;
import com.xms.web.framework.model.JsonResultObject;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author migo
 * @since 2021-08-27
 */
@RestController
@RequestMapping("/{org}/api/schema/optionset")
public class OptionSetFinderController extends ApiBaseController {
    private IOptionSetFinderService _optionSetFinderService;
    private IOptionSetDetailFinderService _optionSetDetailFinderService;
    @Autowired
    public OptionSetFinderController(IOptionSetFinderService optionSetFinderService,
                                     IOptionSetDetailFinderService optionSetDetailFinderService){
        _optionSetFinderService = optionSetFinderService;
        _optionSetDetailFinderService = optionSetDetailFinderService;
    }

    @ApiOperation("查询选项集列表")
    @GetMapping("list")
    public JsonResultObject get(@RequestParam(value="name", required = false) String name,
                                @RequestParam(value="ispublic", required = false) Boolean ispublic,
                                @RequestParam(value="getall", required = false) Boolean getall,
                                @RequestParam(value="page", required = false) Integer page,
                                @RequestParam(value="pagesize", required = false) Integer pagesize,
                                @RequestParam(value="sortby", required = false) String sortby,
                                @RequestParam(value="sortdirection", required = false) String sortdirection)
    {
        LambdaQueryWrapper<OptionSet> lambdaQueryWrapper = getSortedQueryWrapper(sortby, sortdirection);
        if (StringUtil.notEmpty(name))
        {
            lambdaQueryWrapper.like(OptionSet::getName, name);
        }
        if (ispublic != null)
        {
            lambdaQueryWrapper.eq(OptionSet::getIspublic, ispublic);
        }
        else{
            lambdaQueryWrapper.eq(OptionSet::getIspublic, true);
        }
        if (getall != null && getall) {
            return JOk(_optionSetFinderService.list(lambdaQueryWrapper));
        }
        else if(page == null || pagesize == null){
            return JNotSpecifiedParams("page","pagesize");
        }
        page = (page <= 0 ? 1 : page);
        pagesize = (pagesize <= 0 ? 10 : pagesize);
        IPage<OptionSet> datas = _optionSetFinderService.page(new Page<>(page, pagesize), lambdaQueryWrapper);
        PagedList<OptionSet> result = new PagedList<>(datas.getRecords(), page, pagesize, datas.getTotal(), datas.getPages());

        return JOk(result);
    }

    @ApiOperation("查询选项集")
    @GetMapping(value = "getbyid/{id}")
    public JsonResultObject getById(@PathVariable(value = "id", required = true) UUID id)
    {
        return JOk(_optionSetFinderService.getById(id, true));
    }

    @ApiOperation("查询选项集")
    @GetMapping(value = "getpublic/{ispublic}")
    public JsonResultObject getPublic(@PathVariable(value = "ispublic", required = true) boolean isPublic)
    {
        LambdaQueryWrapper<OptionSet> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OptionSet::getIspublic, isPublic);
        List<OptionSet> result = _optionSetFinderService.list(queryWrapper);
        return JOk(result);
    }

    @ApiOperation("查询选项集选项")
    @GetMapping(value = "getitems/{optionsetid}")
    public JsonResultObject getItems(@PathVariable(value = "optionsetid", required = true) UUID optionsetid)
    {
        return JOk(_optionSetDetailFinderService.getByOptionsetid(optionsetid));
    }
}
